1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package java.lang;
27
28 import java.io.IOException;
29 import java.io.FileInputStream;
30 import java.io.FileOutputStream;
31 import java.lang.ProcessBuilder.Redirect;
32 import java.lang.ProcessBuilder.Redirect;
33
34
35
36
37
38
39
40
41 final class ProcessImpl {
42 private static final sun.misc.JavaIOFileDescriptorAccess fdAccess
43 = sun.misc.SharedSecrets.getJavaIOFileDescriptorAccess();
44
45 private ProcessImpl() {}
46
47 private static byte[] toCString(String s) {
48 if (s == null)
49 return null;
50 byte[] bytes = s.getBytes();
51 byte[] result = new byte[bytes.length + 1];
52 System.arraycopy(bytes, 0,
53 result, 0,
54 bytes.length);
55 result[result.length-1] = (byte)0;
56 return result;
57 }
58
59
60 static Process start(String[] cmdarray,
61 java.util.Map<String,String> environment,
62 String dir,
63 ProcessBuilder.Redirect[] redirects,
64 boolean redirectErrorStream)
65 throws IOException
66 {
67 assert cmdarray != null && cmdarray.length > 0;
68
69
70
71 byte[][] args = new byte[cmdarray.length-1][];
72 int size = args.length;
73 for (int i = 0; i < args.length; i++) {
74 args[i] = cmdarray[i+1].getBytes();
75 size += args[i].length;
76 }
77 byte[] argBlock = new byte[size];
78 int i = 0;
79 for (byte[] arg : args) {
80 System.arraycopy(arg, 0, argBlock, i, arg.length);
81 i += arg.length + 1;
82
83 }
84
85 int[] envc = new int[1];
86 byte[] envBlock = ProcessEnvironment.toEnvironmentBlock(environment, envc);
87
88 int[] std_fds;
89
90 FileInputStream f0 = null;
91 FileOutputStream f1 = null;
92 FileOutputStream f2 = null;
93
94 try {
95 if (redirects == null) {
96 std_fds = new int[] { -1, -1, -1 };
97 } else {
98 std_fds = new int[3];
99
100 if (redirects[0] == Redirect.PIPE)
101 std_fds[0] = -1;
102 else if (redirects[0] == Redirect.INHERIT)
103 std_fds[0] = 0;
104 else {
105 f0 = new FileInputStream(redirects[0].file());
106 std_fds[0] = fdAccess.get(f0.getFD());
107 }
108
109 if (redirects[1] == Redirect.PIPE)
110 std_fds[1] = -1;
111 else if (redirects[1] == Redirect.INHERIT)
112 std_fds[1] = 1;
113 else {
114 f1 = new FileOutputStream(redirects[1].file(),
115 redirects[1].append());
116 std_fds[1] = fdAccess.get(f1.getFD());
117 }
118
119 if (redirects[2] == Redirect.PIPE)
120 std_fds[2] = -1;
121 else if (redirects[2] == Redirect.INHERIT)
122 std_fds[2] = 2;
123 else {
124 f2 = new FileOutputStream(redirects[2].file(),
125 redirects[2].append());
126 std_fds[2] = fdAccess.get(f2.getFD());
127 }
128 }
129
130 return new UNIXProcess
131 (toCString(cmdarray[0]),
132 argBlock, args.length,
133 envBlock, envc[0],
134 toCString(dir),
135 std_fds,
136 redirectErrorStream);
137 } finally {
138
139
140 try { if (f0 != null) f0.close(); }
141 finally {
142 try { if (f1 != null) f1.close(); }
143 finally { if (f2 != null) f2.close(); }
144 }
145 }
146 }
147 }